﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using YiSha.Util;
using YiSha.Util.Model;
using YiSha.Entity;
using YiSha.Model;
using YiSha.Admin.Web.Controllers;
using YiSha.Entity.YiDongYuan;
using YiSha.Business.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Web.Code;
using YiSha.Business.OrganizationManage;
using YiSha.Util.Extension;
using YiSha.Business.Cache;

namespace YiSha.Admin.Web.Areas.YiDongYuan.Controllers
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-16 08:31
    /// 描 述：机械备案申报控制器类
    /// </summary>
    [Area("YiDongYuan")]
    public class MechanicsApplyController : BaseController
    {
        private MechanicsApplyBLL mechanicsApplyBLL = new MechanicsApplyBLL();
        private DataDictDetailCache dataDictDetailCache = new DataDictDetailCache();

        #region 视图功能

        [AuthorizeFilter("yidongyuan:mechanicsapply:view")]
        public ActionResult MechanicsApplyIndex(int status = -1)
        {
            ViewBag.Status = status;
            return View();
        }

        public ActionResult MechanicsApplyForm()
        {
            return View();
        }

        [AuthorizeFilter("yidongyuan:mechanicsapply:audit")]
        public ActionResult Audit(long id)
        {
            var entity = mechanicsApplyBLL.GetEntity(id).Result;
            var dict = dataDictDetailCache.GetList().Result.Find(t => t.DictType == "Region" && t.DictValue == entity.Data.Region);
            ViewBag.Code = $"{entity.Data.PFJD}-{dict.DictKey}";
            //ViewBag.Code = $"{dict.DictKey.Replace("4", "")}";
            return View();
        }

        #endregion 视图功能

        #region 获取数据

        [HttpGet]
        [AuthorizeFilter("yidongyuan:mechanicsapply:search")]
        public async Task<ActionResult> GetListJson(MechanicsApplyListParam param)
        {
            TData<List<MechanicsApplyEntity>> obj = await mechanicsApplyBLL.GetList(param);
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("yidongyuan:mechanicsapply:search")]
        public async Task<ActionResult> GetPageListJson(MechanicsApplyListParam param, Pagination pagination)
        {
            TData<List<MechanicsApplyEntity>> obj = await mechanicsApplyBLL.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<ActionResult> GetFormJson(long id)
        {
            TData<MechanicsApplyEntity> obj = await mechanicsApplyBLL.GetEntity(id);
            return Json(obj);
        }

        #endregion 获取数据

        #region 提交数据

        [HttpPost]
        [AuthorizeFilter("yidongyuan:mechanicsapply:audit")]
        public async Task<ActionResult> SaveAudit(string id, int auditStatus, string checkDesc, string code, long? detectionUser)
        {
            OperatorInfo user = await Operator.Instance.Current("");
            TData<string> obj = await mechanicsApplyBLL.SaveAudit(id, auditStatus, checkDesc, code, user.UserId.Value, detectionUser);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("yidongyuan:mechanicsapply:add,yidongyuan:mechanicsapply:edit")]
        public async Task<ActionResult> SaveFormJson(MechanicsApplyEntity entity)
        {
            TData<string> obj = await mechanicsApplyBLL.SaveForm(entity);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("yidongyuan:mechanicsapply:delete")]
        public async Task<ActionResult> DeleteFormJson(string ids)
        {
            TData obj = await mechanicsApplyBLL.DeleteForm(ids);
            return Json(obj);
        }

        #endregion 提交数据
    }
}
